# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=gdcm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.8
pkgrel=2
pkgdesc="The Grassroots DICOM library (mingw-w64)"
arch=('any')
url="https://gdcm.sourceforge.io/wiki/index.php"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         #"${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')
source=(#"https://downloads.sourceforge.net/project/gdcm/gdcm%202.x/GDCM%20${pkgver}/gdcm-${pkgver}.tar.bz2"
        gdcm-${pkgver}.tar.gz::https://github.com/malaterre/GDCM/archive/v${pkgver}.tar.gz
        0002-cmake-config-destination.patch
        0004-fix-find-openssl.patch
        0005-fix-cross-initializing.patch
        0006-openjpeg2-visibility.patch
        0007-getopt-global-var.patch)
sha256sums=('47b96be345b1611784f9e65fc39367c7450c9a1ef81c21f8acddfb6207098315'
            '70b1f693e381f4d8e725db7a71df79f2803809cc7b0ac90ea41b1f3c6238c945'
            '77645101ea8425c601fb0717630b334c8a4ced3ea7f3625c11d338c56529e5fb'
            '018b083e74091776afaef9894fcb1c7554b36828f162bc4c9b2ea2a6b154b159'
            '008635edc8c200b2adf8adf33c3439d39706e9b7ed0e644d7d0ebe18d9a020eb'
            'ed9c4020bd39cadf4d78c72aa4be5afaf3d0a743406e8bee6c5936299e5d1d33')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0002-cmake-config-destination.patch
  patch -p1 -i ${srcdir}/0004-fix-find-openssl.patch
  patch -p1 -i ${srcdir}/0005-fix-cross-initializing.patch
  patch -p1 -i ${srcdir}/0006-openjpeg2-visibility.patch
  patch -p1 -i ${srcdir}/0007-getopt-global-var.patch
}

build() {
  msg "Build static version"
  [[ -d "${srcdir}/build-static-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-static-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-static-${MINGW_CHOST}" && cd "${srcdir}/build-static-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SYSTEM_PREFIX_PATH="${MINGW_PREFIX}" \
    -DGDCM_BUILD_SHARED_LIBS=OFF \
    -DGDCM_USE_SYSTEM_ZLIB=ON \
    -DGDCM_USE_SYSTEM_OPENSSL=ON \
    -DGDCM_USE_SYSTEM_UUID=OFF \
    -DGDCM_USE_SYSTEM_EXPAT=ON \
    -DGDCM_USE_SYSTEM_JSON=ON \
    -DGDCM_USE_SYSTEM_PAPYRUS3=OFF \
    -DGDCM_USE_SYSTEM_SOCKETXX=OFF \
    -DGDCM_USE_SYSTEM_LJPEG=OFF \
    -DGDCM_USE_SYSTEM_OPENJPEG=OFF \
    -DGDCM_USE_SYSTEM_CHARLS=OFF \
    -DGDCM_USE_SYSTEM_POPPLER=ON \
    -DGDCM_USE_SYSTEM_LIBXML2=ON \
    -DGDCM_BUILD_EXAMPLES=OFF \
    -DGDCM_BUILD_TESTING=ON \
    -DGDCM_WRAP_PYTHON=OFF \
    -DGDCM_WRAP_PERL=OFF \
    -DGDCM_WRAP_PHP=OFF \
    -DGDCM_WRAP_JAVA=OFF \
    -DGDCM_WRAP_CSHARP=OFF \
    -DGDCM_USE_JPEGLS=ON \
    -DGDCM_DOCUMENTATION=OFF \
    -DGDCM_USE_VTK=OFF \
    -DGDCM_USE_PARAVIEW=OFF \
    -DGDCM_BUILD_APPLICATIONS=ON \
    ../${_realname}-${pkgver}
  make

  msg "Build shared version"
  [[ -d "${srcdir}/build-shared-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-shared-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-shared-${MINGW_CHOST}" && cd "${srcdir}/build-shared-${MINGW_CHOST}"

  LDFLAGS+=" -Wl,--allow-multiple-definition" # Bypass linking with internal getopt and mingw getopt provided

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SYSTEM_PREFIX_PATH="${MINGW_PREFIX}" \
    -DGDCM_BUILD_SHARED_LIBS=ON \
    -DGDCM_USE_SYSTEM_ZLIB=ON \
    -DGDCM_USE_SYSTEM_OPENSSL=ON \
    -DGDCM_USE_SYSTEM_UUID=OFF \
    -DGDCM_USE_SYSTEM_EXPAT=ON \
    -DGDCM_USE_SYSTEM_JSON=ON \
    -DGDCM_USE_SYSTEM_PAPYRUS3=OFF \
    -DGDCM_USE_SYSTEM_SOCKETXX=OFF \
    -DGDCM_USE_SYSTEM_LJPEG=OFF \
    -DGDCM_USE_SYSTEM_OPENJPEG=ON \
    -DGDCM_USE_SYSTEM_CHARLS=OFF \
    -DGDCM_USE_SYSTEM_POPPLER=ON \
    -DGDCM_USE_SYSTEM_LIBXML2=ON \
    -DGDCM_BUILD_EXAMPLES=OFF \
    -DGDCM_BUILD_TESTING=ON \
    -DGDCM_WRAP_PYTHON=OFF \
    -DGDCM_WRAP_PERL=OFF \
    -DGDCM_WRAP_PHP=OFF \
    -DGDCM_WRAP_JAVA=OFF \
    -DGDCM_WRAP_CSHARP=OFF \
    -DGDCM_USE_JPEGLS=ON \
    -DGDCM_DOCUMENTATION=OFF \
    -DGDCM_USE_VTK=OFF \
    -DGDCM_USE_PARAVIEW=OFF \
    -DGDCM_BUILD_APPLICATIONS=ON \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/build-static-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-shared-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
